package com.ccf.business.knowledge.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.common.model.result.CodeEnum;
import org.springframework.stereotype.Service;
import com.common.model.result.PageResult;
import com.common.model.pojo.Query;
import com.service.mybatis.util.Condition;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.service.mybatis.service.impl.SuperServiceImpl;

import lombok.extern.slf4j.Slf4j;

import com.ccf.business.knowledge.model.LabelTaskUser;
import com.ccf.business.knowledge.mapper.LabelTaskUserMapper;
import com.ccf.business.knowledge.service.ILabelTaskUserService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 标注任务和人员关系
 *
 * @author ccf
 * @date 2022-02-15 16:56:10
 */
@Slf4j
@Service
public class LabelTaskUserServiceImpl extends SuperServiceImpl<LabelTaskUserMapper, LabelTaskUser> implements ILabelTaskUserService {
    /**
    * 列表
    * @param query
    * @return
    */
    @Override
    public PageResult<LabelTaskUser> findList(Query query){
        Page<LabelTaskUser> page = page(Condition.getPage(query), Condition.getQueryWrapper(query));
        return PageResult.<LabelTaskUser>builder().datas(page.getRecords()).code(CodeEnum.SUCCESS.getCode()).count(page.getTotal()).build();
    }

    /**
     * 添加用户
     * @param id
     * @param users
     */
    @Override
    public void saveUser(String id, String[] users, boolean isUpdate) {
        List<LabelTaskUser> userList = Lists.newLinkedList();
        for (String userId : users) {
            LabelTaskUser ltu = new LabelTaskUser(id, userId);
            userList.add(ltu);
        }
        if (userList.size() > 0 ){
            if (isUpdate){
                List<LabelTaskUser> oldDatas = list(Wrappers.<LabelTaskUser>query()
                        .lambda().eq(LabelTaskUser::getTaskId, id));
                if (CollUtil.isNotEmpty(oldDatas)){
                    removeByIds(oldDatas.stream().map(e->e.getId()).collect(Collectors.toList()));
                }
            }
            saveBatch(userList);
        }
    }
}
